//
//  MVCircleListView.m
//  MVVMDemo
//
//  Created by ChenJs92 on 16/11/14.
//  Copyright © 2016年 chenjs92. All rights reserved.
//

#import "MVCircleListView.h"
#import "MVCircleListViewModel.h"
#import "MVCircleListTableCell.h"
@interface MVCircleListView () <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) MVCircleListViewModel *viewModel;
@property (strong, nonatomic) UITableView *mainTableView;

@end

@implementation MVCircleListView

-(instancetype)initWithViewModel:(id<MVViewModelProtocol>)viewModel{
    self.viewModel = (MVCircleListViewModel *)viewModel;
    return [super initWithViewModel:viewModel];
}

- (void)updateConstraints {
    
    WS(weakSelf)
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(weakSelf);
    }];
    [super updateConstraints];
}

#pragma mark - private
- (void)yd_setupViews {
    
    [self addSubview:self.mainTableView];
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
}
-(void)yd_bindViewModel{
//    [self.mainTableView reloadData];
     [self.viewModel.refreshDataCommand execute:nil];
    @weakify(self);
    [self.viewModel.reFreshEndSubject subscribeNext:^(id x) {
        @strongify(self);
        [self.mainTableView reloadData];
        [self.mainTableView.mj_header endRefreshing];
        [self.mainTableView.mj_footer endRefreshing];
    }];
}
#pragma mark - delegate

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.viewModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MVCircleListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithUTF8String:object_getClassName([MVCircleListTableCell class])] forIndexPath:indexPath];
    
    if (self.viewModel.dataArray.count > indexPath.row) {
        
        cell.viewModel = self.viewModel.dataArray[indexPath.row];
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.viewModel.cellClickSubject sendNext:nil];
}



-(MVCircleListViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[MVCircleListViewModel alloc] init];
    }
    return _viewModel;
}
-(UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] init];
        _mainTableView.dataSource = self;
        _mainTableView.delegate = self;
        _mainTableView.backgroundColor = [UIColor whiteColor];
        [_mainTableView registerClass:[MVCircleListTableCell class] forCellReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([MVCircleListTableCell class])]];
        WS(weakSelf)
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            //
            [weakSelf.viewModel.refreshDataCommand execute:nil];
            NSLog(@"xia la ====");
        }];
        _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            //
            [weakSelf.viewModel.nextDataCommand execute:nil];
            NSLog(@"shang la ======");
        }];
        
    }
    return _mainTableView;
}
@end
